<%--
  Created by IntelliJ IDEA.
  User: DELL
  Date: 2022/5/7
  Time: 10:43
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <title>欢迎页面-X-admin2.0</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi" />
    <link rel="stylesheet" href="/static/css/font.css">
    <link rel="stylesheet" href="/static/css/xadmin.css">
    <script type="text/javascript" src="https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js"></script>
    <script src="/static/lib/layui/layui.js" charset="utf-8"></script>
    <script type="text/javascript" src="/static/js/xadmin.js"></script>
    <!-- 让IE8/9支持媒体查询，从而兼容栅格 -->
    <!--[if lt IE 9]>
    <script src="https://cdn.staticfile.org/html5shiv/r29/html5.min.js"></script>
    <script src="https://cdn.staticfile.org/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->
</head>

<body>
<div class="x-body">

    <form class="layui-form">
        <div class="layui-form-item">
            <label  class="layui-form-label">
                <span class="x-red">*</span>姓名
            </label>
            <div class="layui-input-inline">
                <input type="hidden" id="userId" name="userId">
                <input type="text" id="name" name="name" required=""
                       autocomplete="off" class="layui-input">
            </div>
            <div class="layui-form-mid layui-word-aux">
                <span class="x-red">*</span>
            </div>
        </div>

        <div class="layui-form-item">
            <label  class="layui-form-label">
                <span class="x-red">*</span>性别
            </label>
            <div class="layui-input-inline">
                <input type="text" id="sex" name="sex" required=""
                       autocomplete="off" class="layui-input">
            </div>
            <div class="layui-form-mid layui-word-aux">
                <span class="x-red">*</span>
            </div>
        </div>

        <div class="layui-form-item">
            <label for="address" class="layui-form-label">
                <span class="x-red">*</span>地址
            </label>
            <div class="layui-input-inline">
                <input type="text" id="address" name="address" required=""
                       autocomplete="off" class="layui-input">
            </div>
            <div class="layui-form-mid layui-word-aux">
                <span class="x-red">*</span>
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">
                <span class="x-red">*</span>电话
            </label>
            <div class="layui-input-inline">
                <input type="text" id="phone" name="phone" required="" lay-verify="phone"
                       autocomplete="off" class="layui-input">
            </div>
            <div class="layui-form-mid layui-word-aux">
                <span class="x-red">*</span>
            </div>
        </div>


        <div class="layui-form-item">
            <label for="email" class="layui-form-label">
                <span class="x-red">*</span>邮箱
            </label>
            <div class="layui-input-inline">
                <input type="text" id="email" name="email" required="" lay-verify="email"
                       autocomplete="off" class="layui-input">
            </div>
            <div class="layui-form-mid layui-word-aux">
                <span class="x-red">*</span>
            </div>
        </div>
        <div class="layui-form-item">
            <label for="username" class="layui-form-label">
                <span class="x-red">*</span>用户名
            </label>
            <div class="layui-input-inline">
                <input type="text" id="username" name="username" required="" lay-verify="nikename"
                       autocomplete="off" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label for="password" class="layui-form-label">
                <span class="x-red">*</span>密码
            </label>
            <div class="layui-input-inline">
                <input type="password" id="password" name="password" required="" lay-verify="pass"
                       autocomplete="off" class="layui-input">
            </div>
            <div class="layui-form-mid layui-word-aux">
                6到16个字符
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">角色</label>
            <div class="layui-input-block">
                <input type="checkbox" id="admin" name="role[0]" title="admin" value="admin">
                <input type="checkbox" id="simple" name="role[1]" title="simple" value="simple">
            </div>
        </div>

        <div class="layui-form-item">
            <label  class="layui-form-label">
            </label>
            <button  class="layui-btn" type="button" onclick="updateUser()">
                修改
            </button>
        </div>
    </form>
</div>
<script>
    //定义变量userId,作为参数传递给后端
    var userId = localStorage.getItem("userId");

    function updateUser() {
        var name = $("#name").val();
        var sex = $("#sex").val();
        var address = $("#address").val();
        var phone = $("#phone").val();
        var email = $("#email").val();
        var username = $("#username").val();
        var password = $("#password").val();
        var admin = $("input[name='role[0]']").prop("checked"); //没选中为false
        var simple = $("input[name='role[1]']").prop("checked");
        if (admin == true && simple == true){
            var rname = "admin,simple"
        }
        if (admin == false && simple == true){
            var rname = "simple"
        }
        if (admin == true && simple == false){
            var rname = "admin"
        }
        if (admin == false && simple == false){
            var rname = ""
        }
        $.ajax({
            url:"/user/update",
            type:"post",
            dataType:"json",
            data:{
                userId:userId,
                name:name,
                sex:sex,
                address:address,
                phone:phone,
                email:email,
                username:username,
                password:password,
                rname:rname
            },
            success:function (rs) {
                if (rs.code == 200){
                    layer.alert(rs.message,{icon:6},function () {
                        x_admin_close();
                        parent.window.loadData();
                    })
                }
            },error:function () {
                alert("服务器丢了");
            }
        })
    };

    //取出本地变量存储的值，并进行数据回显--渲染到表格中
    function getLocalStorage(){
        $("#name").val(localStorage.getItem("name"));
        $("#sex").val(localStorage.getItem("sex"));
        $("#address").val(localStorage.getItem("address"));
        $("#phone").val(localStorage.getItem("phone"));
        $("#email").val(localStorage.getItem("email"));
        $("#username").val(localStorage.getItem("username"));
        $("#password").val(localStorage.getItem("password"));
        //根据角色判断复选框是否全选
        if (localStorage.getItem("rname") == "simple"){
            console.log("simple");
            $("input[name='role[1]']").prop("checked","true");
        } else {
            $("input[name='role[0]']").prop("checked","true");
            $("input[name='role[1]']").prop("checked","true");
        }
    };

    //一打开页面显示数据
    $(function () {
        getLocalStorage();
    })

</script>
<script>var _hmt = _hmt || []; (function() {
    var hm = document.createElement("script");
    hm.src = "https://hm.baidu.com/hm.js?b393d153aeb26b46e9431fabaf0f6190";
    var s = document.getElementsByTagName("script")[0];
    s.parentNode.insertBefore(hm, s);
})();</script>
</body>

</html>
